package com.example.yian.controller;

import com.example.yian.entity.ProjectInfo;
import com.example.yian.service.ProjectInfoService;
import com.example.yian.service.WeChatAuthService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/my/setting")
public class ProjectInfoController {

    @Autowired
    private ProjectInfoService projectInfoService;

    @Autowired
    private WeChatAuthService weChatAuthService;

    @PostMapping("/updateProjectInfo")
    public Map<String, Object> updateProjectInfo(@RequestHeader("Authorization") String token, @RequestBody ProjectInfo projectInfo) {
        Map<String, Object> response = new HashMap<>();
        Optional<Long> userIdOpt = weChatAuthService.getUserIdFromToken(token);
        if (userIdOpt.isPresent()) {
            Long userId = userIdOpt.get();
            projectInfo.setUserId(userId);
            ProjectInfo updatedProjectInfo = projectInfoService.addOrUpdateProjectInfo(
                    projectInfo.getProjectName(),
                    projectInfo.getConstructionUnit(),
                    projectInfo.getDesignUnit(),
                    projectInfo.getSupervisionUnit(),
                    projectInfo.getInspectionTeamName(),
                    projectInfo.getInspectionPersonnel(),
                    userId
            );
            response.put("code", 200);
            response.put("msg", "成功");
            response.put("data", updatedProjectInfo);
        } else {
            response.put("code", 401);
            response.put("msg", "未授权");
            response.put("time", Instant.now().toEpochMilli());
            response.put("data", new HashMap<>());
        }
        return response;
    }

    @GetMapping("/getProjectInfo")
    public Map<String, Object> getProjectInfo(@RequestHeader("Authorization") String token) {
        Map<String, Object> response = new HashMap<>();
        Optional<Long> userIdOpt = weChatAuthService.getUserIdFromToken(token);
        if (userIdOpt.isPresent()) {
            Long userId = userIdOpt.get();
            Optional<ProjectInfo> projectInfoOpt = projectInfoService.getProjectInfoByUserId(userId);
            if (projectInfoOpt.isPresent()) {
                response.put("code", 200);
                response.put("msg", "成功");
                response.put("data", projectInfoOpt.get());
            } else {
                response.put("code", 404);
                response.put("msg", "未找到记录");
                response.put("time", Instant.now().toEpochMilli());
                response.put("data", new HashMap<>());
            }
        } else {
            response.put("code", 401);
            response.put("msg", "未授权");
            response.put("time", Instant.now().toEpochMilli());
            response.put("data", new HashMap<>());
        }
        return response;
    }
}

